﻿// *****************************************************************************
// **                   Ladenius MediaCenter TVSeries                         **
// ** ----------------------------------------------------------------------- **
// ** (c) 2008 by Jurjen Ladenius                                             **
// *****************************************************************************

#region Using directives
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ladenius.TheTVDB.Entities;
using Ladenius.TheTVDB.Dao;
#endregion

namespace Ladenius.TheTVDB.Business {
    /// <summary>
    /// Provides business logic for Mirrors
    /// </summary>
    internal static class MirrorHome {

        #region Fields
        private static List<Mirror> _mirrors = null;
        #endregion

        #region Public methods
        /// <summary>
        /// Gets a random mirror that will return series data.
        /// </summary>
        /// <returns>A mirror</returns>
        internal static Mirror Get_Data() {
            return GetRandom(TypeMaskType.XMLFiles);
        }
        /// <summary>
        /// Gets a random mirror that will return series banners.
        /// </summary>
        /// <returns>A mirror</returns>
        internal static Mirror Get_Banner() {
            return GetRandom(TypeMaskType.BannerFiles);
        }
        /// <summary>
        /// Gets a random mirror that will return series files.
        /// </summary>
        /// <returns>A mirror</returns>
        internal static Mirror Get_Files() {
            return GetRandom(TypeMaskType.ZipFiles);
        }

        #endregion

        #region Private methods
        /// <summary>
        /// Gets a random mirror that will return data of type type.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <returns>A random mirror</returns>
        private static Mirror GetRandom(TypeMaskType type) {
            List<Mirror> mirrors = GetAll();
            mirrors = new List<Mirror>(mirrors.Where<Mirror>(d => (d.TypeMask & type) == type));
            Random random = new Random(mirrors.Count);
            return mirrors[random.Next(0, mirrors.Count)];
        }
        /// <summary>
        /// Gets the mirrors.
        /// </summary>
        /// <returns></returns>
        private static List<Mirror> GetAll() {
            if (_mirrors == null) _mirrors = XMLRequester.GetMirrors();
            return _mirrors;
        }



        #endregion


    }
}
